package spring.cloud.tasks.executor.executor.task.task_configuration;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import spring.cloud.tasks.executor.executor.task.TreeCacheListener;

@Slf4j
@Setter
@Getter
class ReloadTaskConfigurationUpdateListener extends TreeCacheListener {

    private final TaskConfigurationListenerHolder taskConfigurationListenerHolder;

    public ReloadTaskConfigurationUpdateListener(TaskConfigurationListenerHolder taskConfigurationListenerHolder) {
        this.taskConfigurationListenerHolder = taskConfigurationListenerHolder;
    }

    @Override
    protected void dataChanged(CuratorFramework curatorFramework, String path, TreeCacheEvent treeCacheEvent) {
        if (taskConfigurationListenerHolder.isShutdown) {
            return;
        }
        if (TaskConfigurationNode.isBaseInfoNodePath(taskConfigurationListenerHolder.taskId, path) && (TreeCacheEvent.Type.NODE_ADDED == treeCacheEvent.getType() || TreeCacheEvent.Type.NODE_UPDATED == treeCacheEvent.getType())) {
            taskConfigurationListenerHolder.taskConfiguration.reloadTaskBaseInfoDto();
        }
    }
}
